// flat
var arr = [1, [2, 3, [4, 5]], 6, 7, 8]
var flatArr = arr.flat(Infinity)
console.log(flatArr);
// 正则
var str = JSON.stringify(arr)
var newArr = str.replace(/(\[|\])/g, '').split(',')
console.log(newArr);
// 递归
var result = []
function flat (arr) {
  for (var i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      flat(arr[i])
    } else {
      result.push(arr[i])
    }
  }
}
flat(arr)
console.log(result);